package com.casic.modules.sys.controller;

import java.io.*;
import java.util.*;

import com.casic.common.utils.R;
import com.casic.common.base.entity.PageEntity;
import com.casic.common.utils.UploadFileUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.casic.modules.sys.entity.SysEnclosureEntity;
import com.casic.modules.sys.service.SysEnclosureService;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 系统附件信息表
 * 
 * @author 颜金星
 * @email 123456789@qq.com
 * @date 2017-12-21 15:07:40
 */
@RestController
@RequestMapping("sysenclosure")
public class SysEnclosureController {
	@Autowired
	private SysEnclosureService sysEnclosureService;
	@Value("${spring.uploadFilePath}")
	private String  path;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sysenclosure:list")
	public R list(@RequestParam Map<String, Object> params){
        PageEntity<SysEnclosureEntity> page=sysEnclosureService.findPage(params);
		return R.ok().put("page", page);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sysenclosure:info")
	public R info(@PathVariable("id") String id){
		SysEnclosureEntity sysEnclosure = sysEnclosureService.findById(id);
		return R.ok().put("sysEnclosure", sysEnclosure);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sysenclosure:save")
	public R save(@RequestBody SysEnclosureEntity sysEnclosure){
		sysEnclosureService.save(sysEnclosure);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sysenclosure:update")
	public R update(@RequestBody SysEnclosureEntity sysEnclosure){
		sysEnclosureService.edit(sysEnclosure);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sysenclosure:delete")
	public R delete(@RequestBody String[] ids){
		sysEnclosureService.removeByIds(ids);
		return R.ok();
	}

	/**
	 *  上传文件到本地
	 * @param file
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/uploadFile")
	public R uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
		SysEnclosureEntity sysEnclosureEntity=UploadFileUtil.uploadFile(file,path);
		sysEnclosureEntity =sysEnclosureService.save(sysEnclosureEntity);
		Map<String,Object> map=new HashMap<>();
		map.put("sysEnclosureEntity",sysEnclosureEntity);
		return R.ok(map);
	}
	
}
